package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.ACCID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.HOSTAUDIT;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.IMTOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USERID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USER_NAME;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.DeleteDtBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.dialog.TipDialog;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.AlertDialogUtils;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.LoginUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.luoxiang.socialandroid.utils.NetUtils2;
import com.luoxiang.socialandroid.view.ExpandTextView2;
import com.lxj.xpopup.XPopup;
import com.netease.yunxin.kit.corekit.im.IMKitClient;
import com.netease.yunxin.kit.corekit.im.login.LoginCallback;
import com.stx.xhb.xbanner.XBanner;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class GedtActivity extends BaseActivity {
    private String domain;
    private int userId;
    private String title;
    private int dymainId;
    private XBanner xb;
    private TextView tv_number, text_time, likecount;
    private List<String> bannerList;
    private ImageView img_sandian;
    private String token;
    private int code;
    private ExpandTextView2 tv_titles;
    private String aduitTime;
    private String likeCountnum;
    private int islike;
    private ImageView ic_islike, ic_back;

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_gedt);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);
        tv_titles = findViewById(R.id.tv_titles);
        ic_islike = findViewById(R.id.ic_islike);
        ic_back = findViewById(R.id.ic_back);
        Intent intent = getIntent();
        domain = intent.getStringExtra("domain");
        userId = intent.getIntExtra("userId", 0);
        bannerList = (List<String>) intent.getSerializableExtra("bannerList");
        dymainId = intent.getIntExtra("dymainId", 0);
        title = intent.getStringExtra("title");
        likeCountnum = intent.getStringExtra("likecount");
        aduitTime = intent.getStringExtra("AduitTime");
        islike = intent.getIntExtra("islike", -1);
        xb = findViewById(R.id.banner);
        img_sandian = findViewById(R.id.img_sandian);
        tv_number = findViewById(R.id.tv_number);
        text_time = findViewById(R.id.text_time);
        likecount = findViewById(R.id.likecount);
        tv_titles.setText(title);
        tv_titles.setTextColor(Color.parseColor("#FFFFFF"));
        text_time.setText(aduitTime);
        likecount.setText(likeCountnum);
        if (islike == 0) {
            //代表没有点赞
            ic_islike.setImageResource(R.mipmap.icon_aixinbai);
        } else {
            //代表已经点赞
            ic_islike.setImageResource(R.mipmap.lovatrue);
        }
        ic_islike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (islike == 0) {
                    //代表没有点赞
                    islike = 1;
                    ic_islike.setImageResource(R.mipmap.lovatrue);
                    Integer integer = Integer.valueOf(likeCountnum);
                    integer++;
                    likeCountnum = integer.toString();
                    likecount.setText(likeCountnum);
                    getlikeDynamic(EXTRA, token, dymainId, islike);
                } else {
                    //代表已经点赞
                    islike = 0;
                    ic_islike.setImageResource(R.mipmap.icon_aixinbai);
                    Integer integer = Integer.valueOf(likeCountnum);
                    integer--;
                    likeCountnum = integer.toString();
                    likecount.setText(likeCountnum);
                    getlikeDynamic(EXTRA, token, dymainId, islike);
                }
            }
        });
    }

    /**
     * 点赞/取消点赞的方法
     */
    private void getlikeDynamic(String extra, String token, Integer id, Integer like) {
        NetUtils2.getInstance().getApiService().getlikeDynamic(extra, token, id, like)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr", decryptStr);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    protected void initListener() {
        if (xb != null) {
            xb.setData(bannerList, null);
            xb.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    tv_number.setText(position + 1 + "/" + bannerList.size());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            xb.setmAdapter((banner, view, position) -> {
                //banner 是一个控件，load需要的是url
                Glide.with(GedtActivity.this)
                        .load("https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/" + bannerList.get(position))
                        .into((ImageView) view);
                ImageView imageView = (ImageView) view;
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

            });

//                xb.setPageTransformer(Transformer.Default);
            xb.setPageChangeDuration(100);
        }
        img_sandian.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                TipDialog tipDialog = new TipDialog(GedtActivity.this);
                tipDialog.setTitle(getString(R.string.mi_caozuotishi));
                tipDialog.setCancel(getString(R.string.mi_quxiao));
                tipDialog.setContent(getString(R.string.mi_shifoushanchugaitiaodongtai));
                tipDialog.setConfirm(getString(R.string.mi_queding));
                tipDialog.setOnCancelClickListener(new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        tipDialog.dismiss();
                        return null;
                    }
                });
                tipDialog.setOnConfirmClickListener(new Function0<Unit>() {
                    @Override
                    public Unit invoke() {
                        getdelDynamic(EXTRA, token, dymainId);
                        if (code == 0) {
                            ToastUtils.showShort(R.string.mi_shanchudongtaichenggong);
                            finish();
                        } else {
                            ToastUtils.showShort(R.string.mi_shanchushibai);
                        }
                        tipDialog.dismiss();
                        return null;
                    }
                });
                new XPopup.Builder(GedtActivity.this)
                        .asCustom(tipDialog)
                        .show();
            }

            @Override
            protected void onFastClick() {

            }
        });
        ic_back.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                finish();
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    private void getdelDynamic(String extra, String token, int dynamicId) {
        NetUtils.getInstance().getApiService().getdelDynamic(extra, token, dynamicId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                            LogUtils.i("decryptStrdelete", decryptStr);
                            Gson gson = new Gson();
                            DeleteDtBean deleteDtBean = gson.fromJson(decryptStr, DeleteDtBean.class);
                            code = deleteDtBean.getCode();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}